//
//  SCStatusViewModel.swift
//  weibo
//
//  Created by 阿城 on 15/12/9.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit

class SCStatusViewModel: NSObject {
    
    var statusMod: SCStatusModel?{
        didSet{
            //method3
            //设置转发微博的内容
            if let name = statusMod?.retweeted_status?.user?.name{
                retweetText = "@\(name): "
            }else{
                retweetText = "(原创微博已删除) "
            }
            if let text = statusMod?.retweeted_status?.text{
                retweetText! += text
            }  
        }
    }
    var vipImage: UIImage?{
        if let num = statusMod?.user?.mbrank{
            return UIImage(named: "common_icon_membership_level\(num)")
        }
        return nil
    }
    var vertifiedImage: UIImage?{
        if let num = statusMod?.user?.verified{
            //认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
            switch num{
            case -1:
                return nil
            case 1:
                return UIImage(named: "avatar_vip")
            case 2,3,5:
                return UIImage(named: "avatar_enterprise_vip")
            case 220:
                return UIImage(named: "avatar_grassroot")
            default:
                return nil
            }
        }
        return nil
    }
    //method1 只调用一次
    lazy var reportString: String = {
        return self.getNumString(self.statusMod?.reposts_count ?? 0, defaultStr: "转发")
    }()
    lazy var commentString: String = {
        return self.getNumString(self.statusMod?.comments_count ?? 0, defaultStr: "评论")
    }()
    //method2 多次调用
    var attitudeString: String{
        return getNumString(statusMod?.attitudes_count ?? 0, defaultStr: "赞")
    }
    var retweetText: String?
    lazy var sourceText: String? = {
        //设置来源
        //source = "<a href=\"http://app.weibo.com/t/feed/5vbZyX\" rel=\"nofollow\">\U7f8e\U98df\U8fbe\U4eba\U63a8\U8350</a>"
        if let source = self.statusMod?.source where source != ""{
            let start = source.rangeOfString("\">")?.endIndex
            let end = source.rangeOfString("</a>")?.startIndex
            return source.substringWithRange(start!..<end!)
        }
        return nil
    }()
    var timeStr: String? {
        //发微博的时间
        if let creatTime = statusMod?.created_at {
            return NSDate.sinceString(creatTime)
        }
        return nil
    }
    
    var originalAttributeStr: NSAttributedString? {
        
        if let text = statusMod?.text{
            
            let match = stringToAttributeString(text)
            self.originalMatchResult = match.result
            return match.attr
        }
        
        return nil
    }
    
    var retweetAttributeStr: NSAttributedString? {
        
        if let text = retweetText{
            let match = stringToAttributeString(text)
            self.retweetMatchResult = match.result
            return match.attr
        }
        
        return nil
    }
    
    var originalMatchResult = [SCMatchResult]()
    var retweetMatchResult = [SCMatchResult]()
    
    private func getNumString(num: Int, defaultStr: String) -> String{

        if num > 0{
            if num > 10000{
                let bigNum = (num / 1000) / 10
                return "\(bigNum)万"
            }else{
                return "\(num)"
            }
        }
        return defaultStr
    }
    
    private func stringToAttributeString(text: String) -> (attr:NSAttributedString, result:[SCMatchResult]){
        let emoticonAttr = NSAttributedString.emoticonWithString(NSAttributedString(string: text), regularStr: "\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]")
    
        let attr = NSMutableAttributedString(attributedString: emoticonAttr)
        var result = [SCMatchResult]()
        
        // 匹配 @
        (attr.string as NSString).enumerateStringsMatchedByRegex("@[^\\W]+") { (_, captureString, cputureRange, _) -> Void in
            
            attr.addAttribute(NSForegroundColorAttributeName, value: SCColor.getColor(80, G: 125, B: 175), range: cputureRange.memory)
            result.append(SCMatchResult(string: captureString.memory! as String, range: cputureRange.memory))
        }
        
        // 匹配链接 --> 短链接 http://R.f
        (attr.string as NSString).enumerateStringsMatchedByRegex("http://[^\\s^\\u4e00-\\u9fa5]+") { (_, captureString, cputureRange, _) -> Void in
            attr.addAttribute(NSForegroundColorAttributeName, value: SCColor.getColor(80, G: 125, B: 175), range: cputureRange.memory)
            result.append(SCMatchResult(string: captureString.memory! as String, range: cputureRange.memory))
        }
        
        // 匹配话题
        (attr.string as NSString).enumerateStringsMatchedByRegex("#[^#]+#") { (_, captureString, cputureRange, _) -> Void in
            attr.addAttribute(NSForegroundColorAttributeName, value: SCColor.getColor(80, G: 125, B: 175), range: cputureRange.memory)
            result.append(SCMatchResult(string: captureString.memory! as String, range: cputureRange.memory))
        }
        
        return (attr, result)
    }
    
}
